package _3_data_struct.HashMap;

import com.google.common.base.CaseFormat;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by bpzj on 6/25/2018.
 */
public class HashMapTest {

    @Test
    public void canKeyIsNull() {
        HashMap map = new HashMap();
        map.put(null, 2);
        System.out.println(map.get(null));
    }

    @Test
    public void canTableKeyIsNull() {
        Hashtable hashtable = new Hashtable();
        hashtable.put(null, 0);
        System.out.println(hashtable.get(null));
    }

    @Test
    public void changeKey() {
        Map<String, String> map = new HashMap<>();
        map.put("old_key", "value");
        Set<String> strings = new HashSet<>(map.keySet());
        for (String str : strings) {
            map.put(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str), map.remove(str));
        }
        System.out.println(map);
    }

    public Map<String, Object> toKeyValuePairs(Object instance) {
        instance.getClass().getDeclaredMethods();
        return Arrays.stream(instance.getClass().getDeclaredMethods())
            .collect(Collectors.toMap(
                Method::getName,
                method -> {
                    try {
                        Object result = method.invoke(instance);
                        return result != null ? result : "";
                    } catch (Exception e) {
                        return "";
                    }
                }));
    }

    @Test
    public void hashMapHowToCalculateHash() {
        HashMap<String, String> hashMap = new HashMap<>(10);
        hashMap.put("xwxwxwxwxwxwxwxwxwxwxw", "1");
        hashMap.put("xwxwxwxwxwxwxwxwxwxwyX", "2");
        hashMap.put("xwxwxwxwxwxwxwxwxwyXxw", "3");
        hashMap.put("xwxwxwxwxwxwxwxwxwyXyX", "4");
        hashMap.put("xwxwxwxwxwxwxwxwxwyXz9", "5");
        hashMap.put("xwxwxwxwxwxwxwxwxwz9xw", "6");
        hashMap.put("xwxwxwxwxwxwxwxwxwz9yX", "7");
        hashMap.put("xwxwxwxwxwxwxwxwxwz9z9", "8");
        hashMap.put("xwxwxwxwxwxwxwxwyXxwxw", "9");
        hashMap.put("xwxwxwxwxwxwxwxwyXxwyX", "10");
        hashMap.put("xwxwxwxwxwxwxwxwz9yXxw", "11");
        hashMap.put("xwxwxwxwxwxwyXz9xwyXz9", "12");
        hashMap.put("xwxwxwxwxwxwyXz9xwz9xw", "13");
        hashMap.put("xwxwxwxwxwxwyXz9xwz9yX", "14");
        hashMap.put("xwxwxwxwxwxwyXz9xwz9z9", "15");
        hashMap.put("xwxwxwxwxwxwyXz9yXxwxw", "16");
        hashMap.put("xwxwxwxwxwxwyXz9yXxwyX", "17");
        hashMap.put("xwxwxwxwxwxwyXz9xwyXyX", "18");
        hashMap.put("xwxwxwxwxwxwyXz9xwyXxw", "19");
        hashMap.put("xwxwxwxwxwxwyXz9xwxwyX", "20");
        hashMap.put("xwxwxwxwxwxwyXz9xwxwz9", "21");
        hashMap.put("xwxwxwxwxwxwyXyXz9z9z9", "22");
        hashMap.put("xwxwxwxwxwxwyXyXz9z9yX", "23");
        hashMap.put("xwxwxwxwxwxwyXyXz9z9xw", "24");
        hashMap.put("xwxwxwxwxwxwyXyXz9yXz9", "25");
        hashMap.put("xwxwxwxwxwxwyXyXz9yXyX", "26");
        hashMap.put("xwxwxwxwxwxwyXyXz9yXxw", "27");
        hashMap.put("xwxwxwxwxwxwyXyXz9xwz9", "28");
        hashMap.put("xwxwxwxwxwxwyXyXz9xwyX", "29");
        hashMap.put("xwxwxwxwxwxwyXyXz9xwxw", "30");
        hashMap.put("xwxwxwxwxwxwyXyXyXz9z9", "31");
        String s = hashMap.get("1");
        s = hashMap.get("xwxwxwxwxwxwxwxwyXxwyX");
        System.out.println(hashMap);
    }



}